﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace WebUI
{
    public class ImageProcessor
    {
        public static Image resizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Image b = ScaleImage(imgToResize, new Size(destWidth, destHeight));

            return (Image)b;
        }

        public static Image ScaleImage(Image imgToScale, Size size)
        {
            Bitmap b = new Bitmap(size.Width, size.Height);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
           
            g.DrawImage(imgToScale, 0, 0, size.Width, size.Height);
            g.Dispose();

            return (Image)b;
        }

        public static Image ScaleImage(Image imgToScale, int rateInPercent)
        {
            int newW = imgToScale.Width * rateInPercent / 100;
            int newH = imgToScale.Height * rateInPercent / 100;

            return (Image)ScaleImage(imgToScale, new Size(newW, newH));
        }

        public static Image CropImage(Image imgToScale, int left, int top, int width, int height)
        {
            Bitmap b = new Bitmap(width, height);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToScale, 0, 0, new RectangleF(left, top, width, height), GraphicsUnit.Pixel) ;
            g.Dispose();

            return (Image)b;
        }

        public static Image CreateWatermarkedPhotograph(Image originalImage,Image embeddingImage,string text)
        {
            int phWidth = originalImage.Width;
            int phHeight = originalImage.Height;

            //int wmWidth = embeddingImage.Width;
            //int wmHeight = embeddingImage.Height;

            Bitmap result = new Bitmap(phWidth, phHeight,PixelFormat.Format24bppRgb);
            Graphics grResult = Graphics.FromImage(result);
            grResult.SmoothingMode = SmoothingMode.AntiAlias;

            //Draw original image to result.
            grResult.DrawImage(
               originalImage,                             
               new Rectangle(0, 0, phWidth, phHeight), 
               0,                                       
               0,                                      
               phWidth,                               
               phHeight,                               
               GraphicsUnit.Pixel);

            //Draw text string to result.
            //Font font = null;
            //SizeF textSize = new SizeF();

            //for (int i = 1; i < 50; i++)
            //{
            //    font = new Font("arial", i, FontStyle.Bold);
            //    textSize = grResult.MeasureString(text, font);
                

            //}

            //Draw embedding Image to result.
            //float nPercent = 0;
            //float nPercentW = 0;
            //float nPercentH = 0;
            //if (embeddingImage.Width > (int)(originalImage.Width * .05))
            //{
            // //   nPercentW = 
            //}
            //if (embeddingImage.Height > (int)(originalImage.Height * .05))
            //{

            //}
            int embedddingImageWidth = (int)(originalImage.Width * .25);
            int embeddingImageHeight = (int)(originalImage.Height * .25);

            Image resizedEmbeddingImage = resizeImage(embeddingImage, new Size(embedddingImageWidth, embeddingImageHeight));
            ImageAttributes imageAttributes = new ImageAttributes();

            //The first step in manipulating the watermark image is to replace 
            //the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
            //to do this we will use a Colormap and use this to define a RemapTable
            ColorMap colorMap = new ColorMap();

            //My watermark was defined with a background of 100% Green this will
            //be the color we search for and replace with transparency
            colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

            ColorMap[] remapTable = { colorMap };

            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

            //The second color manipulation is used to change the opacity of the 
            //watermark.  This is done by applying a 5x5 matrix that contains the 
            //coordinates for the RGBA space.  By setting the 3rd row and 3rd column 
            //to 0.3f we achive a level of opacity
            float[][] colorMatrixElements = { 
												new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},       
												new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},        
												new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},        
												new float[] {0.0f,  0.0f,  0.0f,  0.3f, 0.0f},        
												new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}};
            ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);

            imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
                ColorAdjustType.Bitmap);

            int xPosOfWm = ((phWidth - embedddingImageWidth) - 10);
            int yPosOfWm = 10;

            grResult.DrawImage(resizedEmbeddingImage,
                new Rectangle(xPosOfWm, yPosOfWm, embedddingImageWidth, embeddingImageHeight),  //Set the detination Position
                0,                  // x-coordinate of the portion of the source image to draw. 
                0,                  // y-coordinate of the portion of the source image to draw. 
                embedddingImageWidth,            // Watermark Width
                embeddingImageHeight,		    // Watermark Height
                GraphicsUnit.Pixel, // Unit of measurment
                imageAttributes);



            grResult.Dispose();

            resizedEmbeddingImage.Dispose();

            return result;
        }
    }
}
